#ifndef FUNCTION_DEMO_HPP
#define FUNCTION_DEMO_HPP

#include <iostream>
#include <string>
#include <functional>
#include <memory>

namespace test {

class function_demo
{
private:
    /* data */
public:
    function_demo(/* args */);
    ~function_demo();

    void login(const std::string& token, const std::function<void(int, std::string)> result);
    void login2(const std::string& token, const std::function<void(int, std::string)>* result);
};

function_demo::function_demo(/* args */)
{
}

function_demo::~function_demo()
{
}

void function_demo::login(const std::string& token, std::function<void(int, std::string)> result) {
    std::cout << "token:" << token << std::endl;
    result(1, token);
}
void function_demo::login2(const std::string& token, const std::function<void(int, std::string)>* result) {
    std::cout << "token:" << token << std::endl;
    (*result)(1, token);
    delete result;
}

void test_function_demo() {
    function_demo().login("token", [](int code, std::string ext){
        std::cout << code << ext << std::endl;
    });
    auto lambda = [](int code, std::string ext){
        std::cout << code << ext << std::endl;
    };

    const std::function<void(int, std::string)>* pf = new std::function<void(int, std::string)>([pf](int code, std::string ext){
        std::cout << code << ext << std::endl;
        // delete pf; // error
    });
    // pf->assign(lambda, std::allocator());
    // (*pf)(1, "ok");
    function_demo().login2("token2", pf);
}

}

#endif // FUNCTION_DEMO_HPP